package cn.dnui_xqh414.dnuifood_16110100414;

import androidx.appcompat.app.AppCompatActivity;
import cn.dnui_xqh414.dnuifood_16110100414.bean.RegisterBean;
import cn.dnui_xqh414.dnuifood_16110100414.bean.Userbean;
import cn.dnui_xqh414.dnuifood_16110100414.listener.OnRetrofitListener;
import cn.dnui_xqh414.dnuifood_16110100414.model.UserModel;
import cn.dnui_xqh414.dnuifood_16110100414.service.UserService;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.Toast;

import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;

import java.util.Objects;
import java.util.regex.Pattern;

public class RegisterActivity extends BaseActivity implements OnRetrofitListener<RegisterBean>, View.OnClickListener {

    Button register;
    TextInputEditText rg_username,rg_password,rg_mobile,rg_address,rg_comment;
    TextInputLayout tt_userName,tt_password,tt_Mobile;
    ImageButton imgbt_return;

    private String username, password,address,comment,mobile;

    private  UserModel model;

//    private SharedPreferences userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    void initViews() {
        setContentView(R.layout.activity_register);

        register = findViewById(R.id.bt_reregister);

        tt_password = findViewById(R.id.tt_rg_password);
        tt_userName = findViewById(R.id.tt_rg_username);
        tt_Mobile = findViewById(R.id.tt_Mobile);

        rg_username = findViewById(R.id.et_rgusername);
        rg_password = findViewById(R.id.et_rgpassword);
        rg_mobile = findViewById(R.id.et_mobile);
        rg_address = findViewById(R.id.et_address);
        rg_comment = findViewById(R.id.et_comment);

        imgbt_return = findViewById(R.id.imgbt_return);
    }

    @Override
    void initEvents() {

        register.setOnClickListener(this);

        imgbt_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    void initData() {

    }

    @Override
    public void onClick(View v) {
        tt_userName.setErrorEnabled(false);
        tt_password.setErrorEnabled(false);

        username = rg_username.getText().toString();
        password = rg_password.getText().toString();
        mobile = rg_mobile.getText().toString();
        address = rg_address.getText().toString();
        comment = rg_comment.getText().toString();

// TODO: 12/11/2018 有空了就判断一哈其他几个选项填的正不正确
        if (vailUsername(username)&&vailPassword(password)&&vailMobileNum(mobile)){

            model = new UserModel();
            model.register(username, password, mobile, address, comment,
                    this);
        }

    }

    @Override
    public void onSuccess(RegisterBean object, int flag) {
        if (flag == 2 && object.getSuccess().equals("1")){
            Toast.makeText(this, "success", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
            intent.putExtra("rg_username",username);
            intent.putExtra("rg_password",password);
            startActivity(intent);
        }else
        Toast.makeText(this, "username or password not current", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFailure(String msg) {

        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        }


    private boolean vailMobileNum(String MobileNum){
        String p_num = "^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$";
        if (MobileNum.isEmpty()){
            showError(tt_Mobile,"please input the Num");
            return false;
        }
        if (!Pattern.matches(p_num,MobileNum)){
            showError(tt_Mobile,"Please input correct Num");
            return false;
        }
        return true;
    }

    private boolean vailUsername(String userName){
        String p_username = "[a-zA-Z0-9_]{1,16}";

        if (userName.isEmpty()){
            showError(tt_userName, "please input the username");
            return false;
        }
        if (!Pattern.matches(p_username,userName)){
            showError(tt_userName, "username must use a-z, 0-9, underline and keep character in the 16");
            return false;
        }
        return true;
    }

    private boolean vailPassword(String password){
        String p_password = "[a-zA-Z0-9]{1,18}";

        if (password.isEmpty()){
            showError(tt_password, "please input the password");
            return false;
        }
        if(password.length()>18||password.length()<6){
            showError(tt_password, "password should between 18 and 6 characters");
            return false;
        }
        if (!Pattern.matches(p_password,password)){
            showError(tt_password, "password must use a-z, 0-9");
            return false;
        }
        return true;
    }

    private void showError(TextInputLayout layout, String error){
        layout.setError(error);
        Objects.requireNonNull(layout.getEditText()).setFocusable(true);
        layout.getEditText().setFocusableInTouchMode(true);
        layout.getEditText().requestFocus();
    }
}
